/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
	CREATES:	${out_tables}/T15A_ols_iv.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/

	* Load data
	use "${dt_final}/panel_RR_indexes.dta", clear


	* Establish macros for regressions

/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
	
	* Outcomes
	*-----------------------------------------------------------
	local childoutcomes  "zDCCSPunt zptevir_irtscore2 zt_score zcbcl_t_e zcbcl_t_i zibatt_sspi zibatt_ssai zibatt_sssr zas_raw_score" 
	local behaviors "home_score se_score disc_score" 
	local beliefs "zpstyle1 zpstyle2 zpstyle3 zPSCS zPSSS_family zPSSS_friends zPSSS_others zPSI_Distress zCESD zpacotis"	
	local child "zptevir_irtscore2 zcog_index zsoc_index" 

	* Controls
	*-----------------------------------------------------------

	macro  def Xvar1 "i.age_year i.gender"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
	macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  

	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------

	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"
	

/*******************************************************************************
	Table A16: Selection on Unobservables 
*******************************************************************************/

local finaloutcomes "zptevir_irtscore2 zsoc_index" 
foreach var of local finaloutcomes {
	xi: xtreg `var' group2 group3 $Xvar1 if treat2==1, fe i(CENTRO_SALUD)
	estimates store `var'_b
	test group2 group3
	estadd scalar p1=r(p)
	test group2=group3
	estadd scalar p2=r(p)
					
	xi: xtreg `var' group2 group3 $Xvar1 if treat3==1, fe i(CENTRO_SALUD)
	estimates store `var'_i
	test group2 group3
	estadd scalar p1=r(p)
	test group2=group3
	estadd scalar p2=r(p)

	xi: xtreg `var' group2 group3 $Xvar1 if treat2==0 & treat3==0, fe i(CENTRO_SALUD)
	estimates store `var'_u
	test group2 group3
	estadd scalar p1=r(p)
	test group2=group3
	estadd scalar p2=r(p)

	xi: xtreg `var' treat2 treat3 $Xvar1, fe i(CENTRO_SALUD)
	estimates store `var'_ols
	test treat2=treat3
	estadd scalar p2=r(p)

	xi: xtivreg `var' (treat2 treat3=group2 group3) $Xvar1, fe i(CENTRO_SALUD)
	estimates store `var'_iv
	test treat2=treat3
	estadd scalar p2=r(p)					
	}		

local intermediate "zfbeh_gsem zfbel_gsem"

foreach var of local intermediate {				
				*treated nep B
				xi: xtreg `var' i.TIPO  $Xvar1 if  treat2==1 & x==1, fe i(CENTRO_SALUD)
				estimates store `var'_b
				test _ITIPO_2 _ITIPO_3
				estadd scalar p1=r(p)
				test _ITIPO_2==_ITIPO_3
				estadd scalar p2=r(p)	
				
				xi: xtreg `var' i.TIPO  $Xvar1 if  treat3==1 & x==1, fe i(CENTRO_SALUD)
				estimates store `var'_i
				test _ITIPO_2 _ITIPO_3
				estadd scalar p1=r(p)
				test _ITIPO_2==_ITIPO_3
				estadd scalar p2=r(p)		
				
				*untreated
				xi: xtreg `var' i.TIPO  $Xvar1 if  treat2==0 & treat3==0 & x==1,  fe i(CENTRO_SALUD)
				estimates store `var'_u	
				test _ITIPO_2 _ITIPO_3
				estadd scalar p1=r(p)
				test _ITIPO_2==_ITIPO_3
				estadd scalar p2=r(p)		
				
				* ols
				xi: xtreg `var' treat2 treat3  $Xvar1 if x==1,  fe i(CENTRO_SALUD)
				estimates store `var'_ols	
				test treat2==treat3
				estadd scalar p2=r(p)	
				
				* iv
				xtivreg `var' (treat2 treat3=group2 group3) $Xvar1 if x==1,  fe i(CENTRO_SALUD)
				estimates store `var'_iv	
				test treat2==treat3
				estadd scalar p2=r(p)	
				
			}
			
					
	esttab  zptevir_irtscore2_ols  zsoc_index_ols  /*
	*/ zfbeh_gsem_ols  zfbel_gsem_ols  /*
	*/ using "${out_tables}/appendix/T15A_ols_iv.csv", replace f /*
	*/ keep(treat2 treat3) /*
	*/ cells("b(fmt(3)star)" "se(fmt(3)par)") /*
	*/ star(* 0.10 ** 0.05 *** 0.01) /*
	*/ stats( N, fmt(3 3 0) /*
	*/ labels("Observations")) legend	/*
	*/ mtitles("Vocabulary Index" "Socioemotional Index" "Parental Behavioural Index""Parental Beliefs Index" ) /*
	*/ title("OLS") /*
	*/ varlabels(treat2 "NEP-B" treat3 "NEP-I") 
								
	esttab   zptevir_irtscore2_iv  zsoc_index_iv /*
	*/  zfbeh_gsem_iv  zfbel_gsem_iv /*
	*/ using "${out_tables}/appendix/T15A_ols_iv.csv", append f /*
	*/ keep(treat2 treat3) /*
	*/ cells("b(fmt(3)star)" "se(fmt(3)par)") /*
	*/ star(* 0.10 ** 0.05 *** 0.01) /*
	*/ stats(N, fmt(3 3 0) /*
	*/ labels("Observations")) legend	/*
	*/ mtitles("Vocabulary Index" "Socioemotional Index" "Parental Behavioural Index""Parental Beliefs Index" ) /*
	*/ title("IV")	/*
	*/ varlabels(treat2 "NEP-B" treat3 "NEP-I") 

